package lion.framework.core.db.paging;

import lion.framework.core.db.anno.SQLPaging;

/**
 * DB2数据库的分页 行数从1开始记
 * 
 * <pre>
 * time   2013 2013-4-24 上午11:28:10 
 * mail   hl_0074@sina.com
 * </pre>
 * 
 * @author lion
 */
@SQLPaging("db2")
public class DB2SQLPaging implements ISQLPaging {

	@Override
	public String paging(String sql, int start, int size) {

		start = start - 1;
		if (start <= 0) {
			start = 0;
		}
		if (size < 0) { return sql; }

		StringBuilder buf = new StringBuilder();
		buf.append("SELECT * FROM  ( ");
		buf.append("SELECT _B.*, ROWNUMBER() OVER() AS RN FROM  (  ");
		buf.append(sql);
		buf.append(") AS _B  )AS __A WHERE __A.RN BETWEEN " + start + " AND " + size);

		return buf.toString();
	}
}
